
var  cart ={};

//获取购物车列表

cart.cartList =  function  (req, res, next,conn){
  if (req.session.islogin==true) {
  var sql='select * from shopping where user_id='+req.session.user_id;
  //select *,articles.id as id,catalog.name as cata_name from articles left join catalog on articles.cata_id=catalog.id order by articles.id desc
    conn.query(sql,function(err, rows, fields){
  islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
      res.render('shopping',{buy:rows,shopping_num:req.session.shopping_num,islogin:islogin})
      
    }) 
   }else{
    islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
    res.render('shopping',{buy:0,shopping_num:req.session.shopping_num,islogin:islogin})
   }
}

//添加商品
cart.cartAdd   = function (req, res,next,conn){  
  if(!req.session.islogin){ 
     res.send('nologin');
 
     
  }
 //注入
  var sql='insert into shopping values(null,"'+req.session.user_id+'","'+req.body.abstract+'","'+req.body.price+'")';
  console.log(sql);

      conn.query(sql,function(err, rows, fields){   
       conn.query('select count(*) from shopping  where user_id='+req.session.user_id+'',function(err, rows, fields){   
        req.shopping_num = rows[0]['count(*)'];
        req.session.shopping_num=req.shopping_num;
       res.send(""+req.shopping_num);
      })
    })
}


module.exports = cart;
 